package com.bdtop.dbl.ui.activities.device;
///**
// * @FileName: RadarModeActivity.java
// * @Package:com.bdtop.dbl.ui.activities.device
// * @Description: TODO
// * @author: MingjunLi
// * @date:2014-9-28
// * @version V1.0
// */
//
//package com.bdtop.dbl.ui.activities.device;
//
//import android.annotation.TargetApi;
//import android.bluetooth.BluetoothDevice;
//import android.content.BroadcastReceiver;
//import android.content.ComponentName;
//import android.content.Context;
//import android.content.Intent;
//import android.content.IntentFilter;
//import android.content.ServiceConnection;
//import android.os.Build;
//import android.os.Bundle;
//import android.os.IBinder;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//
//import com.bdtop.dbl.BaseActivity;
//import com.bdtop.dbl.R;
//import com.bdtop.dbl.services.BluetoothLeService;
//import com.bdtop.dbl.ui.widgets.RadarView;
//import com.bdtop.dbl.ui.widgets.TitleBar;
//import com.bdtop.dbl.utils.DBLDebug;
//import com.bdtop.dbl.utils.DblUtils;
//
//import java.util.Timer;
//import java.util.TimerTask;
//
///**
// * @ClassName: RadarModeActivity
// * @Description: TODO
// * @author: MingjunLi
// * @date:2014-9-28
// */
//@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
//public class RadarModeActivity extends BaseActivity {
//
//    private static final String KEY_DEVICE = "key_device";
//
//    private TitleBar mTitleBar;
//
//    private RadarView mRadarView;
//    private Button mCallDeviceBtn;
//    private Button mRadarSettingsBtn;
//
//    private BluetoothDevice mDevice;
//    private BluetoothLeService mBluetoothLeService;
//    private boolean mRemoteConnected = false;
//
//    private Timer mScanTimer;
//
//    public static void launch(Context context, BluetoothDevice selectedDevice) {
//        Intent intent = new Intent(context, RadarModeActivity.class);
//        intent.putExtra(KEY_DEVICE, selectedDevice);
//        context.startActivity(intent);
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//
//        setContentView(R.layout.activity_radar_mode);
//
//        mDevice = getIntent().getParcelableExtra(KEY_DEVICE);
//
//        initViews();
//
//        // bind BLE service
//        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
//        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//
//        final IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
//        intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
//        intentFilter.addAction(BluetoothLeService.ACTION_READ_REMOTE_RSSI);
//        registerReceiver(mGattUpdateReceiver, intentFilter);
//
//        mRadarView.startScanning();
//
//        mScanTimer = new Timer();
//        mScanTimer.schedule(new GetRssiTimerTask(), 0, 2000);
//    }
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//
//        unregisterReceiver(mGattUpdateReceiver);
//        mRadarView.stopScanning();
//
//        mScanTimer.cancel();
//        mScanTimer.purge();
//        mScanTimer = null;
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        unbindService(mServiceConnection);
//        mBluetoothLeService = null;
//    }
//
//    private void initViews() {
//        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
//        mTitleBar.setTitle(R.string.radar_mode_title);
//        mTitleBar.setBelongActivity(this);
//
//        mRadarView = (RadarView) findViewById(R.id.radar_view);
//
//        mCallDeviceBtn = (Button) findViewById(R.id.call_device);
//        mCallDeviceBtn.setOnClickListener(mOnClickListener);
//
//        mRadarSettingsBtn = (Button) findViewById(R.id.radar_settings);
//        mRadarSettingsBtn.setOnClickListener(mOnClickListener);
//    }
//
//    private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            final String action = intent.getAction();
//
//            if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
//                mRemoteConnected = true;
//                DBLDebug.logd("Remote device connected");
//            }
//            else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
//                mRemoteConnected = false;
//                DBLDebug.logd("Remote device disconnected");
//            }
//            else if (BluetoothLeService.ACTION_READ_REMOTE_RSSI.equals(action)) {
//                int rssi = intent.getIntExtra("rssi", 0);
//                DBLDebug.logd("mGattUpdateReceiver, rssi=" + rssi);
//
//                // Toast.makeText(RadarModeActivity.this, "rssi=" + rssi,
//                // Toast.LENGTH_LONG).show();
//                locationBleDevice(rssi);
//            }
//
//        }
//    };
//
//    private void locationBleDevice(int rssi) {
//        mRadarView.setDeviceLocation(DblUtils.convertRssiToDistance(rssi));
//    }
//
//    private OnClickListener mOnClickListener = new OnClickListener() {
//
//        @Override
//        public void onClick(View v) {
//            switch (v.getId()) {
//                case R.id.call_device:
//                    if (mBluetoothLeService != null && mRemoteConnected) {
//                        DBLDebug.logd("call button clicked");
//                        mBluetoothLeService.callRemoteDevice();
//                    }
//                    break;
//
//                case R.id.radar_settings:
//                    RadarSettingsActivity.launch(RadarModeActivity.this);
//                    break;
//
//                default:
//                    break;
//            }
//        }
//    };
//
//    private class GetRssiTimerTask extends TimerTask {
//
//        @Override
//        public void run() {
//            if (mBluetoothLeService != null && mRemoteConnected) {
//                DBLDebug.logd("mGetRssiTimerTask readRemoteRssi");
//                mBluetoothLeService.readRemoteRssi();
//            }
//        }
//    };
//
//    // Code to manage Service lifecycle.
//    private final ServiceConnection mServiceConnection = new ServiceConnection() {
//
//        @Override
//        public void onServiceConnected(ComponentName componentName, IBinder service) {
//            DBLDebug.logd("Radar Mode onServiceConnected");
//
//            mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
//            if (!mBluetoothLeService.initialize()) {
//                DBLDebug.logd("Unable to initialize Bluetooth");
//                finish();
//            }
//            // Automatically connects to the device upon successful start-up
//            // initialization.
//            mBluetoothLeService.connect(mDevice.getAddress());
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName componentName) {
//            mBluetoothLeService = null;
//        }
//    };
//}
